using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class ImageManager
	{
		#region CONSTRUCTORS
		static ImageManager()
		{
			_data = new ImageData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static ImageData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static DataTable Insert( string imgName,  int categoryID,  string uRL1,  string uRL2,  string uRL3,  string uRL4,  string uRL5,  string mime,  string location,  long sMSCount,  string createdBy,  DateTime createdDate,  byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[13];
				values[0] = Util.ReplaceNullTo(imgName, DBNull.Value);
				values[1] = categoryID;
				values[2] = Util.ReplaceNullTo(uRL1, DBNull.Value);
				values[3] = Util.ReplaceNullTo(uRL2, DBNull.Value);
				values[4] = Util.ReplaceNullTo(uRL3, DBNull.Value);
				values[5] = Util.ReplaceNullTo(uRL4, DBNull.Value);
				values[6] = Util.ReplaceNullTo(uRL5, DBNull.Value);
				values[7] = mime;
				values[8] = location;
				values[9] = Util.ReplaceNullTo(sMSCount, DBNull.Value);
				values[10] = Util.ReplaceNullTo(createdBy, DBNull.Value);
				values[11] = Util.ReplaceNullTo(createdDate, DBNull.Value);
				values[12] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable Insert(ImageInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[13];
				values[0] = Util.ReplaceNullTo(info.ImgName, DBNull.Value);
				values[1] = info.CategoryID;
				values[2] = Util.ReplaceNullTo(info.URL1, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.URL2, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.URL3, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.URL4, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.URL5, DBNull.Value);
				values[7] = info.Mime;
				values[8] = info.Location;
				values[9] = Util.ReplaceNullTo(info.SMSCount, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static bool Update( long imgID,  string imgName,  int categoryID,  string uRL1,  string uRL2,  string uRL3,  string uRL4,  string uRL5,  string mime,  string location, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[11];
				values[0] = imgID;
				values[1] = Util.ReplaceNullTo(imgName, DBNull.Value);
				values[2] = categoryID;
				values[3] = Util.ReplaceNullTo(uRL1, DBNull.Value);
				values[4] = Util.ReplaceNullTo(uRL2, DBNull.Value);
				values[5] = Util.ReplaceNullTo(uRL3, DBNull.Value);
				values[6] = Util.ReplaceNullTo(uRL4, DBNull.Value);
				values[7] = Util.ReplaceNullTo(uRL5, DBNull.Value);
				values[8] = mime;
				values[9] = location;
				values[10] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( ImageInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[11];
				values[0] = info.ImgID;
				values[1] = Util.ReplaceNullTo(info.ImgName, DBNull.Value);
				values[2] = info.CategoryID;
				values[3] = Util.ReplaceNullTo(info.URL1, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.URL2, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.URL3, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.URL4, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.URL5, DBNull.Value);
				values[8] = info.Mime;
				values[9] = info.Location;
				values[10] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( long imgID)
		{
			_error = "";
			try
			{
				object[] values = { imgID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( ImageInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.ImgID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static ImageInfo Select( long imgID)
		{
			_error = "";
			try
			{
				object[] values = { imgID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				ImageInfo info = new ImageInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return ImageManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return ImageManager.Search( "", "", pageIndex, pageSize);
		}

        public static DataTable SearchBy(int intCateId, string strName, DateTime fromDate, DateTime toDate, int pageIndex, int pageSize)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = intCateId;
                values[1] = strName;
                values[2] = fromDate;
                values[3] = toDate;

                int startIndex = pageIndex * pageSize;
                return _data.SearchBy(values, startIndex, pageIndex);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }
		
		#endregion
		
	}
}